package com.gavin.model;


import java.io.Serializable;

/**
 * ------------------------------
 * 事务执行结果-不建议外部再调用
 * 外部应用不要调用此对象
 * ------------------------------
 * @author wdm（l311576@sina.com）  @date 2018年3月20日
 * @version 1.0
 */
final public class Result implements Serializable {

	private static final long serialVersionUID = 1L;

	final static public Result OK = new Result(0, "OK", "OK");

	private int nCode;
	private String strText;
	private String strInfo;
	private Throwable throwable;

	public int getCode() {
		return nCode;
	}

	public void setCode(int code) {
		if (this == Result.OK) {
			throw new RuntimeException("Return.OK cannot be modified");
		}
		nCode = code;
	}

	public String getInfo() {
		return strInfo;
	}

	public void setInfo(String strInfo) {
		if (this == Result.OK) {
			throw new RuntimeException("Return.OK cannot be modified");
		}
		this.strInfo = strInfo;
	}

	public String getText() {
		return strText;
	}

	public void setText(String strText) {
		if (this == Result.OK) {
			throw new RuntimeException("Return.OK cannot be modified");
		}
		this.strText = strText;
	}

	public static Result fromModel(Model modelReturn) {
		int nCode = modelReturn.getInteger("nCode");
		String strText = modelReturn.getString("strText");
		String strInfo = modelReturn.getString("strInfo");
		return Result.valueOf(nCode, strText, strInfo);
	}

	public void setThrowable(Throwable throwable) {
		this.throwable = throwable;
	}

	public Throwable getThrowable() {
		return this.throwable;
	}

	public Result() {
		this.nCode = 0;
		this.strText = "";
		this.strInfo = "";
	}

	public Result(int nCode, String strText) {
		this.nCode = nCode;
		this.strText = strText;
		this.strInfo = "";
	}

	public Result(int nCode, String strText, String strInfo) {
		this.nCode = nCode;
		this.strText = strText;
		this.strInfo = strInfo;
	}

	public static Result valueOf(int nCode, String strText) {
		return new Result(nCode, strText);
	}

	public static Result valueOf(int nCode, String strText, String strInfo) {
		return new Result(nCode, strText, strInfo);
	}

	public static Result valueOf(Result ret, String strInfo) {
		return new Result(ret.nCode, ret.strText, strInfo);
	}

	public static Result valueOf(int nCode, String strText, Throwable throwable) {
		Result ret = new Result(nCode, strText);
		ret.setThrowable(throwable);
		return ret;
	}

	public static Result valueOf(int nCode, String strText, String strInfo, Throwable throwable) {
		Result ret = new Result(nCode, strText, strInfo);
		ret.setThrowable(throwable);
		return ret;
	}

	public static Result valueOfOK(String strInfo) {
		return new Result(0, "OK", strInfo);
	}

	public String toString() {
		return nCode + ":" + strInfo;
	}
}
